/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-12-29
 * @Time  : 下午10:38
 * @brief :
 */

P.createPage({
    _data:{
        pn:1,
        refresh:null
    },

    _create:function(){
        var Dialog = require('wap_common:widget/ui/dialog/dialog.js').Dialog,
            util = require('wap_common:widget/ui/libs/util.js').util,
            $page = $('.page-gift'),
            $giftContainer = $page.find('.gift-container'),
            dialog = new Dialog({
                title:'使用说明',
                content:'' +
                    '<div style="text-align: left;line-height: 25px">' +
                    '<div>1、请至电脑版"我的帐户”→”优惠券”里使用。</div>' +
                    '<div>2、活期提额券可以用于提高可购买的活期额度，使用后在不突破最高购买额度的基础上，可增加与提额券等面值的额度。</div>' +
                    '<div>3、活期加息券可以用于提高一天的活期收益率，即实际收益为当日收益＋加息券的收益。</div>' +
                    '</div>'
            });

        this.data({
            util:util,
            $page:$page,
            dialog:dialog,
            $giftContainer:$giftContainer
        });
        this.loadData();

    },

    _init:function(){
        var me = this,
            $page = this.data('$page');

        $page.find('.explain .text').on('touchstart',function(){
            me.data('dialog').show();
        });

    },


    loadData:function(refresh){
        var me = this,
            util = this.data('util'),
            $giftContainer = this.data('$giftContainer'),
            itemTpl = __inline("./item.tmpl");

        $.get("/user/getCouponListApi",{
            pn:me.data('pn'),
            ps:5
        },function(ret,status){
            if(ret.error_no == 0){

                var html = '',
                    list = ret.data.list,
                    pageCount = ret.data.page_count;
                $.each(ret.data.list,function(k,v){
                    html+=itemTpl({item:v,util:util});
                });
                $giftContainer.append(html);

                if(refresh){
                    refresh.afterDataLoading('down');
                    list.length ||  refresh.disable();
                }else if(pageCount>1){
                    me.createRefresh();
                }

                var pn = me.data('pn');
                me.data('pn',++pn);
            }
        },'json');

    },

    createRefresh:function(){
        var me = this;
        $('.body-layout').css('height',window.innerHeight).refresh({
            load: function (dir, type) {
                me.loadData(this);
            }
        });

//            .on('statechange', function (e) {
//                e.preventDefault();
//                var data = e.data,
//                    $elem = data[0],
//                    state = data[1],
//                    dir = data[2];
//                switch (state) {
//                    case 'loaded':
//                        break;
//                    case 'beforeload':
//                        break;
//                    case 'loading':
//                        break;
//                    case 'disable':
//                        $elem.html('没有更多优惠券了');
//                        break;
//                }
//            });
    }

});

